
require_relative 'lib/csdn_project'


Dir.glob(File.join(File.dirname(__FILE__),'projects','*.rb')).each {|f| require f}

url = ENV['JENKINS_URL'] or raise "JENKINS_URL environment variable must be defined!"
user = ENV['MAVEN_GIT_USERNAME'] or raise "MAVEN_GIT_USERNAME environment variable must be defined!"
pass = ENV['MAVEN_GIT_PASSWORD'] or raise "MAVEN_GIT_PASSWORD environment variable must be defined!"

csdn_jenkins_view_name = "CSDN-Jenkins"

require 'jenkins_api_client'
client = JenkinsApi::Client.new(
    server_url: url,
    username: user,
    password: pass,
    follow_redirects: true)

CSDNConfig::Job.all_jobs.each do |job|
	puts job.name

	if !(client.view.exists? csdn_jenkins_view_name)
		client.view.create(csdn_jenkins_view_name)
	end
	
	if client.job.exists? job.name
		client.job.update(job.name, job.to_xml)
	else
		client.job.create(job.name, job.to_xml)
		client.view.add_job(csdn_jenkins_view_name, job.name)
	end
end




